<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
/**
 * 商品出库控制器
 */
class ExportController extends CommonController{
	public function index(){
			$pageNum=I('pageNum',1,intval);//第几页
			$numPerPage=I('numPerPage',10,intval);//每页显示条数
			//获取排序的字段及排序方式
			$orderField=$_POST['_order']?$_POST['_order']:'date';
			$orderDirection=$_POST['_sort']?$_POST['_sort']:'desc';
			//组合排序字符串
			$orders=($orderField.' '.$orderDirection);
			//加载检索的基础数据
			$this->search();
			//检索条件
	        if($_POST['search_where']){
	            $this->search_where=$_POST['search_where'];
	        	$where=$this->_search();
	        }
	        //get方式查看入库明细
	        if(!empty($_GET)){
	        	if(isset($_GET['sequence'])){
	        		$where['sequence']=array('eq',$_GET['sequence']);
	        		$search_where['sequence']['like']=$_GET['sequence'];
	        		$this->assign('search_where',$search_where);
	        	}
	        }
	    	//在where条件中增加未删除的判断条件
	       	$where['del']=array('eq','0');
	       	unset($where['goods_name']);
	       	unset($where['customer_name']);
	       	unset($where['account_sequence']);
			$db=D('ExportView');
			//获取总数据条数
			
			session('excel_where',$where);
			$count=$db->where($where)->count();
			$this->assign('totalCount',$count);//数据的总条数
			$this->assign('numPerPage',$numPerPage);//每页显示多少条
			$this->assign('pageNumShown',6);//页标数字多少个
			$this->assign('currentPage',$pageNum);//当前是第几页
			$page= new \Think\Page($count,$numPerPage);
			$this->list=$db->limit($numPerPage)->Page($pageNum)->order($orders)->where($where)->select();
			//权限菜单的显示与隐藏	
			$this->assign('export_addId',62);//添加
			$this->assign('export_delId',86);//删除
			$this->assign('export_delAllId',87);//批量删除
			$this->assign('export_editId',85);//编辑
			$this->assign('export_confirmId',88);//审核
			$this->assign('export_excel',133);//导出报表
			$this->display();
		
	}
	//商品出库操作界面

	//商品出库数据处理
	public function add(){
		if(!empty($_POST)){
			$arr=$_POST;
			//p($arr);die;			
			$receive=I('receive',string);
			if(!$receive) $this->error('请输入领料人姓名');
			$sender=I('sender',string);
			if(!$sender) $this->error('请输入发料人姓名');
			$customer_code=I('customer_code',string);
			if(!$customer_code) $this->error('请选择领料单位');
			foreach ($arr['goods_number'] as $v) {
				if ($v==0) $this->error('产品数量不能为0');
			}
			//入库商品的总数量
			$count=count($arr['goods_id']);		
			$t=time();
			$date=strtotime($arr['date']);
			$db=M('export');
			$db1=M('number');
			$sql="SELECT nextval('SEQ_CK') seq";
			$seq=($db->query($sql));
			$seq='CK'.$seq[0]['seq'];
			for ($i=0; $i <$count ; $i++) { 
				$code=$arr['goods_id'][$i];
				$pay=$arr['goods_number'][$i];
				$price=$arr['goods_price'][$i];
				$data=array(
					'sequence'=>$seq,
					'date'=>$date,
					'customer_code'=>$customer_code,
					'code_code'=>$code,
					'number'=>$pay,
					'type'=>'1',
					'createtime'=>$t,
					'edittime'=>$t,
					'receive'=>$receive,
					'sender'=>$sender,
					'uid'=>session('uid')
				);
				if($db->add($data)){
					if($db1->where(array('code_code'=>array('eq',$code)))->setInc('pay',$pay)){
						// 商品发出数量增加成功
					}else{
						$this->error('物品出库失败');
					} 
				}else{
					$this->error('发料记录失败');
				}
			}
			$this->success('领料成功');
		}else{
			$this->display();
		}

	}
		//出库日志
	public function record(){
		if(!empty($_POST)){
			$pageNum=I('pageNum',1,intval);//第几页
			$numPerPage=I('numPerPage',10,intval);//每页显示条数
			//获取排序的字段及排序方式
			$orderField=$_POST['_order']?$_POST['_order']:'date';
			$orderDirection=$_POST['_sort']?$_POST['_sort']:'desc';
			//组合排序字符串
			$orders=($orderField.' '.$orderDirection);
			//加载检索的基础数据
			$this->search();
			//检索条件
	        if($_POST['search_where']){
	            $this->search_where=$_POST['search_where'];
	        	$where=$this->_search();
	        }
	    	//在where条件中增加未删除的判断条件
	       	$where['del']=array('eq','0');
			$db=D('ExportView');
			//获取总数据条数
			$count=$db->where($where)->group('sequence')->count();
			$this->assign('totalCount',$count);//数据的总条数
			$this->assign('numPerPage',$numPerPage);//每页显示多少条
			$this->assign('pageNumShown',6);//页标数字多少个
			$this->assign('currentPage',$pageNum);//当前是第几页
			$page= new \Think\Page($count,$numPerPage);
			$field=array('sequence','date','sender','receive','status','uid','depot_name','customer_name','customer_type','sum(number) as sum','sum(number*price) as jine');
	 		$this->list=$db->limit($numPerPage)->Page($pageNum)->order($orders)->where($where)->group('sequence')->field($field)->select();
	 		$this->display();
	 	}else{
	 		$this->search();
	 		$this->display();
	 	}
	}
		//修改商品入库数量及价格，并更新出入库数据
	public function edit(){
		if(!empty($_POST)){
			$db=M(CONTROLLER_NAME);
			$db1=M('number');
			$id=$_POST['id'];
			$code=$_POST['code'];
			$newnumber=I('newnumber',intval);
			$oldnumber=I('oldnumber',intval);
			if($newnumber<>$oldnumber){
				$status=$db->where(array('id'=>$id))->getField('status');
				if($status<>1) $this->error('当前状态不能修改');
				if($db->where(array('id'=>$id))->save(array('number'=>$newnumber,'edittime'=>time(),'uid'=>session('uid')))){
					$number=$newnumber-$oldnumber;
					if($db1->where(array('code_code'=>array('eq',$code)))->setInc('pay',$number)){
						$this->success('更新成功');
					}else{
	                    $this->error ('库存表发出数量更新失败');
	            	}				
				}else{
					$this->error('出库记录更新失败');
				}
			}else{
				$this->error ('更改前后数值没有变化');
			}
		}else{
			$id=$_GET['id'];
			$db=M(CONTROLLER_NAME);
			$field=array('id','number','code_code');
			$this->list=$db->field($field)->find($id);
			$this->display();
		}
	}

	//将记录由正常状态改为删除状态,并将商品出出库数据删除
	public function delete(){
		$id = $_GET['id'];
		if(isset($id)){
			$db=M('export');
			$status=$db->where(array('id'=>$id))->getField('status');
			if($status<>1) $this->error('当前状态不能删除');
			//获得删除出库明细的商品代码及数量
			$inf=$db->field('code_code,number')->find($id);
			if ($db->where(array('id'=>$id))->save(array('del'=>1,'edittime'=>time(),'uid'=>session('uid')))) {
					if (M('number')->where(array('code_code'=>$inf['code_code']))->setDec('pay',$inf['number'])) {
						$this->success ('删除成功');
					}else {
                    	$this->error ('库存表发出数量删除失败');
            		}
                    
                } else {
                    $this->error ('出库记录删除失败');
            }

		}
	}
	//批量删除出库记录，并将商品出库数据恢复
	public function allDelete(){
		if(!empty($_POST)){
			$ids=$_POST['ids'];
			if(isset($ids)){
				$db=M(CONTROLLER_NAME);
				$db1=M('number');

				$status=$db->where(array('id'=>array('in',$ids)))->getField('status',true);
				foreach ($status as $key => $value) {
					if($value<>1) $this->error('当前状态不能删除');
				}
				
				$inf=$db->where(array('id'=>array('in',$ids)))->field(array('id','code_code','number'))->select();
				foreach ($inf as $key => $value) {					
					if($db1->where(array('code_code'=>$value['code_code']))->setDec('pay',$value['number'])){
						//删除成功
					}else{
						$this->error ('出库数量删除失败');
					}
				}
				if($db->where(array('id'=>array('in',$ids)))->save(array('del'=>1,'edittime'=>time(),'uid'=>session('uid')))){
					$this->success ('删除成功');
				}else{
					$this->error ('出库纪录删除失败');
				}

			}
		}
	}
	public function out(){
		$where=session('excel_where');
		$field=array("FROM_UNIXTIME(date,'%Y年%m月%d日') as date",'sequence','code','goods_name','xinghao','guige','unit','number','price','supplier_name','depot_name','sender','receive','customer_name','customer_type',"if(export.status=1,'初始创建','已经审核') as status");
		$db=D('ExportView');
		$data=$db->where($where)->field($field)->order($orders)->select();
		$row=array('sequence','date','code','supplier_name','depot_name','goods_name','xinghao','guige','unit','price','number','customer_name','customer_type','receive','sender','status');
		$data=array_sort($data,$row);
    	//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
		import("Org.Util.PHPExcel");
		import("Org.Util.PHPExcel.Writer.Excel5");
		import("Org.Util.PHPExcel.IOFactory.php");
		$filename="出库明细";
		$headArr=array('出库编码','出库日期','物料编码','供应商名称','仓库名称','物料名称','型号','规格','单位','价格','数量','领料单位','类型','领料人','发料人','状态');
		$this->getExcel($filename,$headArr,$data);
	}
}